package com.yd.controller.sys;


import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.controller.yd.YD_Dongtais;
import com.yd.pojo.Check;
import com.yd.pojo.Sys;
import com.yd.pojo.Users;
import com.yd.service.CheckService;
import com.yd.service.SysService;
import com.yd.service.UsersService;
import com.yd.tool.MD5Hash;
import com.yd.tool.UtsReg;

/**
 * 管理员修改用户信息
 * @author Administrator
 *
 */
@Controller
public class UsersControllerSys {

	@Resource
	private UsersService usersService;
	@Resource
	private SysService sysService;
	@Resource
	private CheckService checkService;
	//管理员修改用户密码
	@RequestMapping("/sys/user/pass/up")
	@ResponseBody
	public String sys_user_pass_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		
		String token = "";
		String loginname = "";
		String npass = "";
		String npass2 = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIntXl(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "loginname 错误");jsonObject.put("error", "1");return jsonObject.toString();}		
		if(UtsReg.getStringPass(request.getParameter("npass"))){npass=request.getParameter("npass").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "新密码 格式错误");return jsonObject.toString();}
		if(UtsReg.getStringPass(request.getParameter("npass2"))){npass2=request.getParameter("npass2").trim();}else{jsonObject.put("error", "1");jsonObject.put("msg", "重复密码 格式错误");return jsonObject.toString();}
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Users users = usersService.selectOne(new EntityWrapper<Users>().eq("loginname", loginname));
		if(users==null){
			jsonObject.put("error", "1");jsonObject.put("msg", "该用户不存在");return jsonObject.toString();
		}
		if(!npass.equals(npass2)){jsonObject.put("error", "1");jsonObject.put("msg", "两次密码不一致");return jsonObject.toString();}
		users.setLoginpass(MD5Hash.getMd5HashPasswod(npass2));
		
		if(!usersService.updateById(users)){
			jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();

	}
	
	//管理员修改用户头像
	@RequestMapping("/sys/user/touxiang/up")
	@ResponseBody
	public String sys_user_touxiang_up(HttpServletRequest request) throws MalformedURLException, IOException {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String img = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getStringKB(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getImg(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "图片格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setImg(img);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		//修改发布动态表里面的头像
	    //调用远程动态表修改头像方法 把用户id和用户img名称传过去
	    //无需判断是否修改成功
		//验证ID
		YD_Dongtais.getUsersByUserid(users.getId()+"");
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员修改用户性别
	@RequestMapping("/sys/user/sex/up")
	@ResponseBody
	public String sys_user_sex_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String sex = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getSex(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		if ("男".equals(sex)) {
			users.setSex(sex);
		}else if("女".equals(sex)){
			users.setSex(sex);
		}else {
			jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员修改用户年龄
	@RequestMapping("/sys/user/age/up")
	@ResponseBody
	public String sys_user_age_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String age = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "性别格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setAge(age);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员修改用户昵称
	@RequestMapping("/sys/user/nicheng/up")
	@ResponseBody
	public String sys_user_nicheng_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String nicheng = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setNicheng(nicheng);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员修改用户名字
	@RequestMapping("/sys/user/name/up")  
	@ResponseBody
	public String sys_user_name_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String name = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringZname(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "名字格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setName(name);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员修改用户邮箱
	@RequestMapping("/sys/user/email/up")
	@ResponseBody
	public String sys_user_email_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String email = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setEmail(email);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员修改用户地址
	@RequestMapping("/sys/user/address/up")
	@ResponseBody
	public String sys_user_address_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String address = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("address"))){address = request.getParameter("address").trim();}else{jsonObject.put("msg", "地址格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		users.setAddress(address);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}

		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员删除用户
	@RequestMapping(value={"/sys/user/delbyuid"})
	@ResponseBody
	public String sys_user_delbyuid(HttpServletRequest request){
		JSONObject jsonObject = new JSONObject(); 
		
		String token="";
		String uid="";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIntXl(request.getParameter("uid"))){uid=request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		if(usersService.selectCount(new EntityWrapper<Users>().eq("id", uid))<1){
			jsonObject.put("msg", "该用户不存在");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Check check = checkService.selectOne(new EntityWrapper<Check>().eq("uid", uid));
		if(check!=null){
			check.deleteById(uid);
		}
		if(!usersService.deleteById(uid)){
			jsonObject.put("msg", "失败2");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("msg", "成功");
		jsonObject.put("error", "0");
		return jsonObject.toString();
	}
	
	//管理员查询热门商家
	@RequestMapping(value = {"/sys/users/list"})
	@ResponseBody
	public String sys_users_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		
		String token = "";
		String tid="";
		String page="";
		String size="";
		
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getInt(request.getParameter("tid"))){tid=request.getParameter("tid").trim();}else{jsonObject.put("msg", "类别格式错误");jsonObject.put("error", "1");}
		if(UtsReg.getStringKB(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getStringKB(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做查询");jsonObject.put("error", "1");return jsonObject.toString();
		}
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));	
		
		List<Users> list = null;
		EntityWrapper<Users> ew=new EntityWrapper<Users>();
		if(tid.length()>0){
			ew.eq("tid", tid);
		}
		
		list=usersService.selectList(ew.eq("tid", tid).orderBy("uptime", false));
		PageInfo<Users> pages = new PageInfo<Users>(list);
		for (int i = 0; i < pages.getList().size(); i++) {
			Users u = pages.getList().get(i);
			u.setToken("");
			u.setLoginpass("");
		}
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");
		return jsonObject.toString();
	}
	
	//管理员提交商户信息
	@RequestMapping("/sys/users/personal/update")
	@ResponseBody
	public String sys_users_personal_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String tid = "";
		String gongsiname = "";
		String gongsilogo = "";
		String gongsitel = "";
		String address = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getInt(request.getParameter("tid"))){tid = request.getParameter("tid").trim();}else{jsonObject.put("msg", "类别格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("gongsiname"))){gongsiname = request.getParameter("gongsiname").trim();}else{jsonObject.put("msg", "公司名字格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getImg(request.getParameter("gongsilogo"))){gongsilogo = request.getParameter("gongsilogo").trim();}else{jsonObject.put("msg", "公司图片格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getInt(request.getParameter("gongsitel"))){gongsitel = request.getParameter("gongsitel").trim();}else{jsonObject.put("msg", "电话格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringKB(request.getParameter("address"))){address = request.getParameter("address").trim();}else{jsonObject.put("msg", "地址格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		Check check = checkService.selectOne(new EntityWrapper<Check>().eq("uid", uid));
		if (checkService.selectOne(new EntityWrapper<Check>().eq("uid", uid))!=null) {
			jsonObject.put("msg", "该資料已提交,请耐心等待...");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		if (checkService.selectOne(new EntityWrapper<Check>().eq("gongsiname", gongsiname))!=null) {
			jsonObject.put("msg", "该名字已存在待审核区,请查看该名字是否正确...");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		if (checkService.selectOne(new EntityWrapper<Check>().eq("gongsitel", gongsitel))!=null) {
			jsonObject.put("msg", "该电话已存在待审核区,请查看电话是否正确...");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		check= new Check();
		check.setUid(uid);
		check.setTid(tid);
		check.setGongsiname(gongsiname);
		check.setGongsilogo(gongsilogo);
		check.setGongsitel(gongsitel);
		check.setAddress(address);
		check.setState("0");
		if (!checkService.insert(check)) {
			jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//商户查询自己的下级
	@RequestMapping("/sys/users/tpid/list")
	@ResponseBody
	public String sys_users_tpid_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请先登陆之后再做修改");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", id));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		List<Users> list = usersService.selectList(new EntityWrapper<Users>().eq("tpid", users.getId()));
		if(list.size()!=0){
			List<String> aa=new ArrayList<String>();//aa=新输出的名字的数组
			for(int i=0;i<list.size();i++){//xiajimz=下级名字
				String xiajimz=list.get(i).getLoginname();
				String xiajinc=list.get(i).getNicheng();
				aa.add(xiajimz);
				aa.add(xiajinc);
			}
			jsonObject.put("msg", "成功");
			jsonObject.put("data",aa);
			return jsonObject.toString();
		}else {
			jsonObject.put("msg", "该商户不存在下级");
			jsonObject.put("error", "1");
		}
		return jsonObject.toString();
	}
	
	//商户资料修改_总
	@RequestMapping("/sys/user/ziliao/update")
	@ResponseBody
	public String sys_merchant_ziliao_up(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String uid = "";
		String email = "";
		String img = "";
		String nicheng = "";
		String name = "";
		String sex = "";
		String age = "";
		String address = "";
		if (UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getIntXl(request.getParameter("uid"))){uid = request.getParameter("uid").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}
		if (UtsReg.getStringEmail(request.getParameter("email"))){email = request.getParameter("email").trim();}else{jsonObject.put("msg", "邮箱格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getImg(request.getParameter("img"))){img = request.getParameter("img").trim();}else{jsonObject.put("msg", "头像格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getStringZname(request.getParameter("name"))){name = request.getParameter("name").trim();}else{jsonObject.put("msg", "名字格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getSex(request.getParameter("sex"))){sex = request.getParameter("sex").trim();}else{jsonObject.put("msg", "性别格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getIntXl(request.getParameter("age"))){age = request.getParameter("age").trim();}else{jsonObject.put("msg", "年龄格式 错误");jsonObject.put("error", "1");}
		if (UtsReg.getStringKB(request.getParameter("address"))){address = request.getParameter("address").trim();}else{jsonObject.put("msg", "地址格式 错误");jsonObject.put("error", "1");}
		//验证管理员是否登录
		Sys sys =sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "重新登录");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", uid));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		users.setEmail(email);
		users.setImg(img);
		users.setName(name);
		users.setNicheng(nicheng);
		if ("男".equals(sex)) {
			users.setSex(sex);
		}else if("女".equals(sex)){
			users.setSex(sex);
		}else {
			jsonObject.put("msg", "只能输入男/女");jsonObject.put("error", "1");return jsonObject.toString();
		}
		users.setAge(age);
		users.setAddress(address);
		if(!usersService.updateById(users)){
			jsonObject.put("msg", "失败");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员查询所有用户
	@RequestMapping("/sys/user/list")
	@ResponseBody
	public String sys_user_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String loginname = "";
		String nicheng = "";
		String tid = "";
		String page = "";
		String size = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIsPhone(request.getParameter("loginname"))){loginname = request.getParameter("loginname").trim();}else{jsonObject.put("msg", "登录名格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getStringKB(request.getParameter("nicheng"))){nicheng = request.getParameter("nicheng").trim();}else{jsonObject.put("msg", "昵称格式错误");jsonObject.put("error", "1");}	
		if(UtsReg.getInt(request.getParameter("tid"))){tid=request.getParameter("tid").trim();}else{jsonObject.put("msg", "类别格式错误");jsonObject.put("error", "1");}
		if(UtsReg.getInt(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getInt(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请重新登陆");jsonObject.put("error", "1");return jsonObject.toString();
		}
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));	
		
		List<Users> list = null;
		EntityWrapper<Users> ew=new EntityWrapper<Users>();
		if(loginname.length()>0){ew.like("loginname", loginname);}
		if(nicheng.length()>0){ew.like("nicheng", nicheng);}
		if(tid.length()>0){ew.like("tid", tid);}
		list = usersService.selectList(ew);
		
		if (list==null) {jsonObject.put("msg", "没有数据");jsonObject.put("error", "1");return jsonObject.toString();}
		PageInfo<Users> pages = new PageInfo<Users>(list);
		
		//将返回的重要信息清空
		for (int i = 0; i < pages.getList().size(); i++) {
			Users u = pages.getList().get(i);
			u.setToken("");
			u.setLoginpass("");
		}
		
		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
		
	//管理员根据id查询用户
	@RequestMapping("/sys/user/findbyid")
	@ResponseBody
	public String sys_user_findbyid(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请重新登陆");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Users users =usersService.selectOne(new EntityWrapper<Users>().eq("id", id));
		if(users==null){
			jsonObject.put("msg", "该商户信息不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		//将返回的重要信息清空
		users.setToken("");
		users.setLoginpass("");
		
		jsonObject.put("data", users);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//管理员_修改商户等级
	@RequestMapping("/sys/merchant/level/update")
	@ResponseBody
	public String sys_merchant_level_update(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		String state = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getInt(request.getParameter("state"))){state = request.getParameter("state").trim();}else{jsonObject.put("msg", "状态格式错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		//判断管理员是否登录
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请重新登陆");jsonObject.put("error", "1");return jsonObject.toString();
		}
		//判断需要修改的用户是否存在
		Users users = usersService.selectOne(new EntityWrapper<Users>().eq("id", id));
		if(users==null){
			jsonObject.put("msg", "该用户不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		//根据id判断审核表是否存在该信息
	 	Check check = checkService.selectOne(new EntityWrapper<Check>().eq("uid", id));
		if (check==null) {jsonObject.put("msg", "请先提交申请");jsonObject.put("error", "1");return jsonObject.toString();}
		//如果传入的审核状态=1,审核失败,修改数据库state状态
		if (state.equals("1")) {
			jsonObject.put("msg", "审核不通过");
			jsonObject.put("error", "0");
			check.setState("1");
			//判断是否成功插入数据库
			if(!checkService.updateById(check)){
				jsonObject.put("msg", "失败");
				jsonObject.put("error", "1");
				return jsonObject.toString();
			}
			return jsonObject.toString();}
		//如果传入的审核状态=2,审核通过,修改数据库state状态
			if (state.equals("2")) {
			jsonObject.put("msg", "审核通过");
			jsonObject.put("error", "0");
			check.setState("2");
			//判断是否成功插入数据库
			if(!checkService.updateById(check)){
				jsonObject.put("msg", "失败");
				jsonObject.put("error", "1");
				return jsonObject.toString();
			}
			//修改用户表tid字段-级别
			users.setTid(check.getTid()); 
			if(!usersService.updateById(users)){
				jsonObject.put("msg", "失败");
				jsonObject.put("error", "1");
				return jsonObject.toString();
			}
			jsonObject.put("error", "0");
			jsonObject.put("msg", "成功");
		}
		
		return jsonObject.toString();

	}
	
	//管理员_查看待审核_用户
	@RequestMapping("/sys/user/check/list")
	@ResponseBody
	public String sys_user_check_list(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String state = "";
		String gongsiname = "";
		String page = "";
		String size = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getInt(request.getParameter("state"))){state=request.getParameter("state").trim();}else{jsonObject.put("msg", "状态格式错误");jsonObject.put("error", "1");}
		if(UtsReg.getStringKB(request.getParameter("gongsiname"))){gongsiname=request.getParameter("gongsiname").trim();}else{jsonObject.put("msg", "公司名格式错误");jsonObject.put("error", "1");}
		if(UtsReg.getInt(request.getParameter("page"))){page=request.getParameter("page").trim();}else{page = "1";}
		if(UtsReg.getInt(request.getParameter("size"))){size=request.getParameter("size").trim();}else{size = "10";}
		
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请重新登陆");jsonObject.put("error", "1");return jsonObject.toString();
		}
		PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));	
		
		List<Check> list = null;
		EntityWrapper<Check> ew=new EntityWrapper<Check>();
		if(gongsiname.length()>0){ew.like("gongsiname", gongsiname);}
		if(state.length()>0){ew.eq("state", state);}
		list = checkService.selectList(ew);
		
		if(list==null){jsonObject.put("msg", "没有数据");jsonObject.put("error", "1");return jsonObject.toString();}
		PageInfo<Check> pages = new PageInfo<Check>(list);

		jsonObject.put("data", pages);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
	//删除待审核用户
	@RequestMapping("/sys/user/check/del")
	@ResponseBody
	public String sys_user_check_del(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getInt(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式错误");jsonObject.put("error", "1");return jsonObject.toString();}
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请重新登陆");jsonObject.put("error", "1");return jsonObject.toString();
		}
		
		Check check = checkService.selectOne(new EntityWrapper<Check>().eq("id", id));
		if(check==null){jsonObject.put("msg", "没有数据");jsonObject.put("error", "1");return jsonObject.toString();}
		if (!checkService.deleteById(check)) {jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();}
		
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
		
	//管理员_查询用户_审核资料详情
	@RequestMapping("/sys/check/findbyid")
	@ResponseBody
	public String sys_check_findbyid(HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();

		String token = "";
		String id = "";
		if(UtsReg.getStringKB(request.getParameter("token"))){token = request.getParameter("token").trim();}else{jsonObject.put("msg", "参数错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		if(UtsReg.getIntXl(request.getParameter("id"))){id = request.getParameter("id").trim();}else{jsonObject.put("msg", "id格式 错误");jsonObject.put("error", "1");return jsonObject.toString();}	
		Sys sys=sysService.selectOne(new EntityWrapper<Sys>().eq("token", token));
		if(sys==null){
			jsonObject.put("msg", "请重新登陆");jsonObject.put("error", "1");return jsonObject.toString();
		}
		Check check =checkService.selectOne(new EntityWrapper<Check>().eq("id", id));
		if(check==null){
			jsonObject.put("msg", "该信息不存在");
			jsonObject.put("error", "1");
			return jsonObject.toString();
		}
		
		jsonObject.put("data", check);
		jsonObject.put("error", "0");
		jsonObject.put("msg", "成功");

		return jsonObject.toString();

	}
	
}
